package assetruleapi

import (
	"encoding/json"

	ledgerapi "github.com/hyperledger/fabric-samples/asset-kx/chaincode-go/chaincode/ledger-api"
)

const (
	RULE_TYPE_ASSET_VALUATION = "RULE_TYPE_ASSET_VALUATION"
	AssetTypeRule = "assetType~ruleType"
)

type Rule struct {
	RuleType       string            `json:"ruleType"`
	RuleDesc       string            `json:"ruleDesc"`
	RuleExpression string            `json:"ruleExpression"`
	RuleParam      map[string]string `json:"ruleParam"`
}

type AssetRule struct {
	AssetType string `json:"assetType"`
	Rule
}


func (b *AssetRule) Keys() []string {
	return []string{b.AssetType, b.RuleType}
}
func (b *AssetRule) Serialize() ([]byte, error) {
	return json.Marshal(b)
}
func (b *AssetRule) DataType() string{
	return AssetTypeRule
}

func UnmarshalStateData(content []byte) (ledgerapi.StateData, error) {
	return Unmarshal(content)
}

func Unmarshal(content []byte) (*AssetRule, error) {
	if len(content) == 0 {
		return nil, nil
	}
	al := &AssetRule{}
	if err := json.Unmarshal(content, al); err != nil {
		return nil, err
	}
	return al, nil
}